<template>
  <div class="home-view">
    <section class="hero-section">
      <div class="hero-content">
        <h1 class="hero-title">欢迎来到电影世界</h1>
        <p class="hero-subtitle">探索精彩电影，分享你的观影体验</p>
        <div class="hero-actions">
          <router-link to="/movies" class="btn primary">浏览电影</router-link>
          <router-link v-if="!authStore.isAuthenticated" to="/login" class="btn secondary">登录/注册</router-link>
          <router-link v-if="authStore.isAuthenticated" to="/add-movie" class="btn secondary">添加电影</router-link>
        </div>
      </div>
    </section>

    <section class="features-section">
      <div class="container">
        <h2 class="section-title">网站功能</h2>
        <div class="features-grid">
          <div class="feature-card">
            <div class="feature-icon">🎬</div>
            <h3 class="feature-title">电影浏览</h3>
            <p class="feature-description">查看最新最热的电影信息，包括详情、评分和演员表</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">📝</div>
            <h3 class="feature-title">发表评论</h3>
            <p class="feature-description">登录后可以对你喜欢的电影发表评论和评分</p>
          </div>
          <div class="feature-card">
            <div class="feature-icon">➕</div>
            <h3 class="feature-title">添加电影</h3>
            <p class="feature-description">登录用户可以添加新的电影信息到我们的数据库</p>
          </div>
        </div>
      </div>
    </section>

    <section class="recent-movies" v-if="movies.length > 0">
      <div class="container">
        <h2 class="section-title">最新电影</h2>
        <div class="movies-grid">
          <MovieCard 
            v-for="movie in recentMovies" 
            :key="movie.id" 
            :movie="movie"
            @click="goToDetail(movie.id)"
          />
        </div>
        <router-link to="/movies" class="view-all-link">查看全部电影 →</router-link>
      </div>
    </section>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import api from '../composables/useApi'
import MovieCard from '../components/MovieCard.vue'

const movies = ref([])
const router = useRouter()
const authStore = useAuthStore()

// 获取最近4部电影
const recentMovies = ref([])

onMounted(async () => {
  try {
    const response = await api.getAllMovies()
    movies.value = response.data
    // 获取最新的4部电影
    recentMovies.value = [...movies.value].sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt)).slice(0, 4)
  } catch (error) {
    console.error('获取电影列表失败:', error)
  }
})

const goToDetail = (id) => {
  router.push({ name: 'movie-detail', params: { id } })
}
</script>

<style scoped>
.home-view {
  padding-bottom: 3rem;
}

.hero-section {
  background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), 
              url('https://images.unsplash.com/photo-1489599849927-2ee91cede3ba?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80');
  background-size: cover;
  background-position: center;
  color: white;
  padding: 6rem 1rem;
  text-align: center;
}

.hero-content {
  max-width: 800px;
  margin: 0 auto;
}

.hero-title {
  font-size: 2.5rem;
  margin-bottom: 1rem;
}

.hero-subtitle {
  font-size: 1.25rem;
  margin-bottom: 2rem;
  opacity: 0.9;
}

.hero-actions {
  display: flex;
  gap: 1rem;
  justify-content: center;
}

.btn {
  padding: 0.75rem 1.5rem;
  border-radius: 4px;
  font-weight: 500;
  text-decoration: none;
  transition: all 0.3s ease;
}

.btn.primary {
  background-color: #42b983;
  color: white;
}

.btn.primary:hover {
  background-color: #3aa876;
}

.btn.secondary {
  background-color: transparent;
  color: white;
  border: 1px solid white;
}

.btn.secondary:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
}

.section-title {
  text-align: center;
  margin: 3rem 0 2rem;
  font-size: 2rem;
  color: #333;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
  margin-top: 2rem;
}

.feature-card {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  text-align: center;
  transition: transform 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-5px);
}

.feature-icon {
  font-size: 2.5rem;
  margin-bottom: 1rem;
}

.feature-title {
  font-size: 1.25rem;
  margin-bottom: 1rem;
  color: #42b983;
}

.feature-description {
  color: #666;
  line-height: 1.6;
}

.movies-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 2rem;
  margin: 2rem 0;
}

.view-all-link {
  display: block;
  text-align: center;
  margin-top: 1rem;
  color: #42b983;
  font-weight: 500;
  text-decoration: none;
}

.view-all-link:hover {
  text-decoration: underline;
}

@media (max-width: 768px) {
  .hero-title {
    font-size: 2rem;
  }
  
  .hero-subtitle {
    font-size: 1.1rem;
  }
  
  .hero-actions {
    flex-direction: column;
    align-items: center;
  }
  
  .btn {
    width: 100%;
    max-width: 200px;
  }
  
  .features-grid {
    grid-template-columns: 1fr;
  }
  
  .movies-grid {
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  }
}
</style>